package net.zigin.nhi.could.waste.service.impl;

import net.zigin.commons.util.ReflectUtil;
import net.zigin.commons.util.StringUtil;
import net.zigin.nhi.could.waste.dao.HospitalPlaceMapper;
import net.zigin.nhi.could.waste.dao.WasteBaseMapper;
import net.zigin.nhi.could.waste.model.WasteBase;
import net.zigin.nhi.could.waste.vo.WasteBaseRecordVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.zigin.nhi.could.waste.dao.BaseMapper;
import net.zigin.nhi.could.waste.dao.WasteBaseRecordMapper;
import net.zigin.nhi.could.waste.model.WasteBaseRecord;
import net.zigin.nhi.could.waste.service.WasteBaseRecordService;

import java.util.ArrayList;
import java.util.List;

/*#################################################################*
# 说明:以下代码通过哲勤代码生成工具自动生成    
# 工具:GeneratorCode
# 作者:mgw1986                                                  
# 生成时间:2021-9-20 17:18:00                                               
##################################################################*/


/**
 * WasteRecord服务实现类
 *
 * @author mgw1986
 * @version V1.0
 * @ClassName: WasteRecordServiceImpl
 * @date 2021-9-20 17:18:00
 */
@Service
public class WasteBaseRecordServiceImpl extends AbstractServiceImpl<WasteBaseRecord, String> implements WasteBaseRecordService {
    @Autowired
    private WasteBaseRecordMapper wasteBaseRecordMapper;
    @Autowired
    private WasteBaseMapper wasteBaseMapper;
    @Autowired
    private HospitalPlaceMapper hospitalPlaceMapper;

    @Override
    public BaseMapper<WasteBaseRecord, String> getMapper() {
        return wasteBaseRecordMapper;
    }

    @Override
    public List<WasteBaseRecordVo> getListByWasteId(String wasteBaseId) {
        // todo box warn
        WasteBase wasteBase = wasteBaseMapper.selectByPrimaryKey(wasteBaseId);
        List<WasteBaseRecordVo> wasteBaseRecordVos;
        WasteBaseRecordVo basic = new WasteBaseRecordVo(wasteBase);
        String placeUserStaffName = hospitalPlaceMapper.getStaffNameById(wasteBase.getHospitalPlaceId());
        List<WasteBaseRecord> list = wasteBaseRecordMapper.getListByWasteBaseId(wasteBaseId);
        if (!list.isEmpty()) {
            wasteBaseRecordVos = ReflectUtil.copyProperties(list, WasteBaseRecordVo.class);
        } else {
            wasteBaseRecordVos = new ArrayList<>();
        }
        wasteBaseRecordVos.forEach(wasteBaseRecordVo -> {
            if (wasteBaseRecordVo.getType().equals("do_collect")) {
                wasteBaseRecordVo.setCollectStaffName(wasteBase.getCollectUserStaffName());
                wasteBaseRecordVo.setHandUserStaffName(wasteBase.getHandUserStaffName());
            } else if (wasteBaseRecordVo.getType().equals("in_depot")) {
                wasteBaseRecordVo.setHospitalPlaceName(wasteBase.getHospitalPlaceName());
                if(!StringUtil.isNullOrEmpty(wasteBase.getWasteBoxCode())){
                    wasteBaseRecordVo.setWasteBoxCode(wasteBase.getWasteBoxCode());
                }
                if(!StringUtil.isNullOrEmpty(placeUserStaffName)){
                    wasteBaseRecordVo.setPlaceUserStaffName(placeUserStaffName);
                }
            } else if (wasteBaseRecordVo.getType().equals("out_depot")) {
                wasteBaseRecordVo.setUserRevicerName(wasteBase.getUserRevicerName());
                if(!StringUtil.isNullOrEmpty(placeUserStaffName)){
                    wasteBaseRecordVo.setPlaceUserStaffName(placeUserStaffName);
                }
                if(!StringUtil.isNullOrEmpty(wasteBase.getWasteBoxCode())){
                    wasteBaseRecordVo.setWasteBoxCode(wasteBase.getWasteBoxCode());
                }
            }
        });
        wasteBaseRecordVos.add(basic);
        return wasteBaseRecordVos;
    }
}